package patterndemo.observer;

import java.util.Vector;

public abstract class Subject {
    private Vector<Observer> obs = new Vector();

    public abstract void doSomething();

    public void addObserver(Observer observer) {
        this.obs.add(observer);
    }

    public void delObserver(Observer observer) {
        this.obs.remove(observer);
    }

    protected void notifyObserver() {
        for (Observer ob : obs) {
            ob.update();
        }
    }

}

class ConcreteSubject extends Subject {
    @Override
    public void doSomething() {
        System.out.println("被观察者事件发生");
        this.notifyObserver();
    }
}

interface Observer {
    void update();
}

class ConcreteObserverA implements Observer {

    @Override
    public void update() {
        System.out.println("观察者A收到信息，并进行处理");
    }
}

class ConcreteObserverB implements Observer {

    @Override
    public void update() {
        System.out.println("观察者B收到信息，并进行处理");
    }
}

class Client {
    public static void main(String[] args) {
        Subject sub = new ConcreteSubject();
        sub.addObserver(new ConcreteObserverA());
        sub.addObserver(new ConcreteObserverB());
        sub.doSomething();
    }
}


